import{goodlist}from "./good.js" 
var wrap=document.getElementsByClassName("wrap")[0] 
var good_id=location.search.split("=")[1]
goodlist.showdetail(good_id).then(function(e){
    var goodinfo=JSON.parse(e)[0]
    console.log(goodinfo)
    var subpicList=goodinfo.goods_sub_pic.split(",")
    var html=`<div class="left">
              <div class="smallBox">
                  <img src="http://${goodinfo.goods_pic}"
                      alt="">
              </div>
              <ul class="imgList">
                  <li><img src="http://${subpicList[0]}"
                          alt=""></li>
                  <li><img src="http://${subpicList[0]}"
                          alt=""></li>
                  <li><img src="http://${subpicList[0]}"
                          alt=""></li>
              </ul>
          </div>
          <div class="right">
              <h2>${goodinfo.goods_title}</h2>
              <p>
                  商品价格： <span>￥${goodinfo.goods_current_price}</span>
              </p>
              <p>
                  交易数量： <span>${goodinfo.goods_trade_num}</span>
              </p>
              <p><button class="reduce">-</button><input type="num" value="1" class="num"><button class="add">+</button></p>
              <p>
                  <button class="buyNow">立即购买</button>
                  <button class="addCar">加入购物车</button>
              </p>
          </div> `
      wrap.innerHTML=html;
      var addCar=document.getElementsByClassName("addCar")[0]
      addCar.onclick=function(){
          var user=localStorage.getItem("user")
          var pwd=localStorage.getItem("pwd")
          var user_id=localStorage.getItem("user_id")
          if(user&&pwd&&user_id){
                  var goods_num=document.getElementsByClassName("num")[0].value
                  var goods_title=goodinfo.goods_title
                  var goods_current_price=goodinfo.goods_current_price
                  var goods_pic=goodinfo.goods_pic
                  goodlist.addCar(user_id,user,good_id,goods_title,goods_num,goods_current_price,goods_pic).then(function(result){
                    if(result.status){
                        if(confirm("加入购物车成功，是否跳转购物车")){
                            location.href ="shoppingcar.html?user_id="+user_id
                        }
                    }
                  })
            //       $.ajax({data:{
            //           user_id:user_id,
            //           user_name:user,
            //           goods_id:good_id,
            //           goods_name:goods_title,
            //           goods_num:goods_num,
            //           goods_price:goods_current_price,
            //           goods_pic:goods_pic
            //       },
            //   url:"addtocar.php",
            //   dataType:"json",
            //   callback:function(result){
            //       if(result.status){
            //           if(confirm("加入购物车成功，是否跳转购物车")){
            //               location.href ="shoppingcar.html?user_id="+user_id
            //           }
            //       }
            //   }})
              }
          else{
              localStorage.setItem("backurl",location.href)
              location.href="login.html"
          }
      }
})

